package com.atguigu.dao.impl;

import java.util.List;

import com.atguigu.dao.BookDao;
import com.atguigu.pojo.Book;

public class BookDaoImpl extends BaseDaoimpl<Book> implements BookDao {

	@Override
	public int saveBook(Book book) {
		String sql = "insert into t_book(`name`,`author`,`price`,`sales`,`stock`,`img_path`) values(?,?,?,?,?,?)";
		return update(sql, book.getName(),book.getAuthor(),book.getPrice(),
				book.getSales(),book.getStock(),book.getImgPath());
	}

	@Override
	public int update(Book book) {
		String sql = "update t_book set `name` = ?,`author` = ?,`price` = ?,`sales` = ?,"
				+ "`stock` = ?,`img_path` = ? where id = ?";
		return update(sql, book.getName(),book.getAuthor(),
				book.getPrice(),book.getSales(),book.getStock(),
				book.getImgPath(),book.getId());
	}

	@Override
	public int deleteBook(int id) {
		String sql = "delete from t_book where id = ?";
		return update(sql, id);
	}

	@Override
	public Book queryBookOne(int id) {
		String sql = "select `id`, `name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath from t_book where id = ?";
		return 	getOne(sql, id);
	}

	@Override
	public List<Book> queryAllBook() {
		String sql = "select `id`, `name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath from t_book ";
		return 	getList(sql);
	}

	@Override
	public int queryCount() {
		String sql = "select count(*) from t_book";
		Long count = (Long) getCount(sql);
		return count.intValue();
	}

	@Override
	public List<Book> queryPageItems(int begin, int pageSize) {
		String sql = "select `id`, `name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath from t_book limit ?,? ";
		return getList(sql, begin,pageSize);
	}

	@Override
	public int queryCountByPrice(double min, double max) {
		String sql = "select count(*) from t_book where price between ? and ?";
		Long count = (Long) getCount(sql,min,max);
		return count.intValue();
	}

	@Override
	public List<Book> queryPageItemsByPrice(int begin, int pageSize, double min, double max) {
		String sql = "select `id`, `name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath "
				+ "from t_book where price between ? and ? order by price limit ?,? ";
		return getList(sql, min, max, begin, pageSize);
	}


}
